package com.phone580.app.main;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

import com.phone580.app.BaseActivity;
import com.phone580.app.BaseTask.TaskCallback;
import com.phone580.app.CommonData;
import com.phone580.app.R;
import com.phone580.app.download.DownloadManagerActivity;
import com.phone580.app.entity.LogoutEntity;
import com.phone580.app.hotapp.HotAppActivity;
import com.phone580.app.hotgame.HotGameActivity;
import com.phone580.app.login.LoginActivity;
import com.phone580.app.login.LogoutTask;
import com.phone580.app.setting.SettingActivity;
import com.phone580.app.sysmanager.SysManagerActivity;

public class MainActivity extends TabActivity implements TaskCallback{
	private TabHost m_tabHost;
	private LayoutInflater mLayoutInflater;
	private String[] item = { "手机应用", "热门游戏", "系统管家", "下载管理" };
	private int[] icon = {R.drawable.main_tab_app_selector, R.drawable.main_tab_game_selector,
			R.drawable.main_tab_sys_selector, R.drawable.main_tab_down_selector};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		init();
//		m_tabHost.setCurrentTab(1);
	}

	private void init() {
		m_tabHost = getTabHost();
		mLayoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		for (int i = 0; i < item.length; i++) {
			TabSpec tabSpec = m_tabHost.newTabSpec(i + "")
					.setIndicator(getTabItemView(i))
					.setContent(getTabItemIntent(i));
			m_tabHost.addTab(tabSpec);
		}
	}

	private View getTabItemView(int index) {
		View view = mLayoutInflater.inflate(R.layout.main_tab_item, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.main_tab_item_icon_iv);
		imageView.setImageResource(icon[index]);
//		TextView textView = (TextView) view.findViewById(R.id.main_tab_item_title_tv);
//		textView.setText(item[index]);
//		textView.setVisibility(View.GONE);
		return view;
	}

	private Intent getTabItemIntent(int index) {
		Intent intent = null;
		switch (index) {
		case 0:
			intent = new Intent(this, HotAppActivity.class);
			break;
		case 1:
			intent = new Intent(this, HotGameActivity.class);
			break;
		case 2:
			intent = new Intent(this, SysManagerActivity.class);
			break;
		case 3:
			intent = new Intent(this, DownloadManagerActivity.class);
			break;
		}
		return intent;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("设置").setIcon(R.drawable.menu_setting);
		if (CommonData.login != null) {
			menu.add("注销").setIcon(R.drawable.menu_logout);
		} else {
			menu.add("登录").setIcon(R.drawable.menu_logout);
		}
		menu.add("退出").setIcon(R.drawable.menu_exit);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getTitle().equals("设置")) {
			Intent intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
		}
		if (item.getTitle().equals("退出")) {
			System.exit(0);
		}
		if (item.getTitle().equals("登录")) {
			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
		}
		if (item.getTitle().equals("注销")) {
			if (CommonData.login == null) {
				Toast.makeText(this, "你还没登录", Toast.LENGTH_SHORT).show();
			} else {
				LogoutTask task = new LogoutTask(this);
				task.setTaskCallback(this);
				task.execute();
			}
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void execute(Object result) {
		if (result != null) {
			if (result instanceof LogoutEntity) {
				LogoutEntity entity = (LogoutEntity)result;
				Toast.makeText(this, entity.getMessage(), Toast.LENGTH_SHORT).show();
				Activity activity = getLocalActivityManager().getCurrentActivity();
				if (activity instanceof HotAppActivity || activity instanceof HotGameActivity) {
					((BaseActivity)activity).userLogonOrLogout();
				}
			}
		} else {
			Toast.makeText(this, "网络错误", Toast.LENGTH_SHORT).show();
		}
	}

}
